﻿using CustomerServiceCenter.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using BusinessLayer;
using System.Web;
using CustomerServiceCenter.Helper;


namespace CustomerServiceCenter.Controllers.API
{
    public class RatingsController : ApiController
    {
        /// <summary>
        /// ajax 分页  获取ratings信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [LoginAuthorize]
        public RatingsModelAndMaxPage GetTicketsAndRatings(RatingsPCModel model)
        {
            DateTime startDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime endDate = DateTime.Now;
            var loginInfo = HttpContext.Current.Session["adminLoginInfo"] as Operator;
            long[] applicationID = ProjectBusiness.GetApplicationIDOrOperatorID(loginInfo.ID);
            //获取总页数
            int[] statusArray = new int[] { Convert.ToInt32(TicketStatusType.Closed)};
            int maxPage = TicketBusiness.GetCount(statusArray, model.project, startDate, endDate, model.operatorId, 1, applicationID);
            if (maxPage < model.pageSize)
            {
                maxPage = 1;
            }
            else
            {
                maxPage = maxPage % model.pageSize == 0 ? maxPage / model.pageSize : maxPage / model.pageSize + 1;
            }
            List<VTicketsRequestInfo> ticketInfo = TicketBusiness.FindTickets(statusArray, model.project, model.operatorId, startDate, endDate, model.pageIndex, model.pageSize, 1, applicationID);
            List<RatingsModel> ticketRatingsInfo = new List<RatingsModel>();
            foreach (var item in ticketInfo)
            {
                RatingsModel ratings = new RatingsModel();
                ratings.Member = Convert.ToInt32(item.AssignerInfo.ID);
                ratings.MemberName = TicketBusiness.GetOperatorInfo((long)item.AssignerInfo.ID).FirstOrDefault().Name;
                ratings.Project = item.ApplicationID;
                ratings.ProjectName = ProjectBusiness.GetApplicationInfo(item.ApplicationID).First().Name;
                ratings.Rating = Convert.ToInt32(item.ServiceRate);
                ticketRatingsInfo.Add(ratings);
            }

            RatingsModelAndMaxPage ratingsAndMaxPage = new RatingsModelAndMaxPage()
            {
                Model = ticketRatingsInfo,
                MaxPage = maxPage,
            };

            return ratingsAndMaxPage;
        }


        /// <summary>
        /// ajax 查询条件
        /// </summary>
        /// <returns></returns>
       [LoginAuthorize]
        public RatingSelectModel GetTicketsInfoByOperator()
        {

            var loginInfo = HttpContext.Current.Session["adminLoginInfo"] as Operator;
            //获得项目ID数组，并找到包含这些项目的TICKETS
            long[] applicationID = ProjectBusiness.GetApplicationIDOrOperatorID(loginInfo.ID);
            List<VTicketsRequestInfo> ticketInfo = TicketBusiness.GetTicketsInfoByOperator(loginInfo.RoleID, applicationID);
            Dictionary<int, RatingsModel> ticketMemberInfo = new Dictionary<int, RatingsModel>();
            Dictionary<int, RatingsModel> ticketProjectInfo = new Dictionary<int, RatingsModel>();
            foreach (var item in ticketInfo)
            {
                //判断该ticket是否已经安排工程师
                if (item.AssignerInfo.ID != null && item.AssignerInfo.ID != 0)
                {
                    RatingsModel ratings = new RatingsModel();
                    ratings.Member = Convert.ToInt32(item.AssignerInfo.ID);
                    ratings.MemberName = TicketBusiness.GetOperatorInfo((long)item.AssignerInfo.ID).FirstOrDefault().Name;
                    ratings.Project = item.ApplicationID;
                    ratings.ProjectName = ProjectBusiness.GetApplicationInfo(item.ApplicationID).First().Name;
                    //去除重复工程师名，项目名
                    if (!ticketMemberInfo.ContainsKey(Convert.ToInt32(item.AssignerInfo.ID)))
                    {
                        ticketMemberInfo.Add(Convert.ToInt32(item.AssignerInfo.ID), ratings);
                    }
                    if (!ticketProjectInfo.ContainsKey(Convert.ToInt32(item.ApplicationID)))
                    {
                        ticketProjectInfo.Add(Convert.ToInt32(item.ApplicationID), ratings);
                    }
                }
            }
            RatingSelectModel selectModel = new RatingSelectModel()
            {
                MemberInfo = ticketMemberInfo,
                ProjectInfo = ticketProjectInfo
            };

            return selectModel;
        }



    }
}
